package com.example.javalib.skill;

import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.applet.*;

public class Heart extends Applet {
    int width=568;
    int height=350; //声明int类型变量
    Image image;//声明图像变量
    Graphics draw_Curve;//声明图形绘制变量
    public void init(){//Applect程序初始化
        setBackground(Color.black);
        setSize(width,height);
        image=createImage(width,height);
        if (image!=null) {
            draw_Curve=image.getGraphics();
        }

    }
    @Override
    public void paint(Graphics g){
        //利用Graphics绘制组件
        draw_Curve.clearRect(0, 0, width, height);//用制定颜色填充清除指定的矩形
        draw_Curve.setColor(Color.red);
        int i,j;
        double x,y,r;
        for(i=0;i<=90;i++)//笛卡尔数学公式:(x*x+y*y-2ax）2=4a*a(x*x+y*y)
            for(j=0;j<=90;j++){
                r=Math.PI/45*i*(1-Math.sin(Math.PI/45*j))*18;
                x=r*Math.cos(Math.PI/45*j)*Math.sin(Math.PI/45*i)+width/2;
                y=-r*Math.sin(Math.PI/45*j)+height/4;
                draw_Curve.fillOval((int)x, (int)y, 2, 2);
            }
        g.drawImage(image,0,0,this);
    }
    public static void main(String[] args){
        Heart heart=new Heart();
        JFrame jFrame = new JFrame();
        jFrame.setSize(600 , 600);
        jFrame.setVisible(true);
        jFrame.add(heart);

        heart.init();
        System.out.println("笛卡尔心形曲线已成功显示");
    }

}
